# -*- coding: utf8 -*-
__author__ = 'bpatel'
import wx
import tkMessageBox
from Events import Events
from Utils import Utils
# begin wxGlade: dependencies
# end wxGlade

# begin wxGlade: extracode

# end wxGlade
ID_ABOUT = 101
ID_REBOOT=103
ID_EXIT = 102
ID_RESET_IP=104
class UIFrame(wx.Frame):
    
    def __init__(self, *args, **kwds):

        self.utils = Utils()
        self.host = ""
        self.port = "8080"
        self.events = Events(self.host, self.port)
     
        kwds["style"] = wx.DEFAULT_FRAME_STYLE ^ (wx.RESIZE_BORDER | wx.MAXIMIZE_BOX)

        wx.Frame.__init__(self, *args, **kwds)
        self.statusbar = self.CreateStatusBar()
        self.Centre()

        try:
            self.host = self.utils.getRokuAddress()
            self.setIPAddress(self.host)
        except:
            self.host = self.changeIP()
        
        menu = wx.Menu()
        menu.Append(ID_RESET_IP, "Roku &IP","Reset Roku IP")

        menu.Append(ID_REBOOT, "&Reboot",
                     "Reboot Roku")
        menu.Append(ID_ABOUT, "&About",
                     "More information about this program")
        menu.AppendSeparator()
        menu.Append(ID_EXIT, "E&xit", "Terminate the program")
        menuBar = wx.MenuBar()
        menuBar.Append(menu, "&File");
        
        self.SetMenuBar(menuBar)
        self.bitmap_button_back = wx.BitmapButton(self, -1, wx.Bitmap("images/back.jpg", wx.BITMAP_TYPE_ANY))
        self.bitmap_button_home = wx.BitmapButton(self, -1, wx.Bitmap("images/home.png", wx.BITMAP_TYPE_ANY))
        self.bitmap_button_up = wx.BitmapButton(self, -1, wx.Bitmap("images/up.png", wx.BITMAP_TYPE_ANY))
        self.bitmap_button_left = wx.BitmapButton(self, -1, wx.Bitmap("images/left.png", wx.BITMAP_TYPE_ANY))
        self.bitmap_button_ok = wx.BitmapButton(self, -1, wx.Bitmap("images/ok.png", wx.BITMAP_TYPE_ANY))
        self.bitmap_button_right = wx.BitmapButton(self, -1, wx.Bitmap("images/right.png", wx.BITMAP_TYPE_ANY))
        self.bitmap_button_down = wx.BitmapButton(self, -1, wx.Bitmap("images/down.png", wx.BITMAP_TYPE_ANY))
        self.bitmap_button_info = wx.BitmapButton(self, -1, wx.Bitmap("images/info.png", wx.BITMAP_TYPE_ANY))
        self.bitmap_button_rewind = wx.BitmapButton(self, -1, wx.Bitmap("images/rewind.png", wx.BITMAP_TYPE_ANY))
        self.bitmap_button_play = wx.BitmapButton(self, -1, wx.Bitmap("images/play.png", wx.BITMAP_TYPE_ANY))
        self.bitmap_button_forward = wx.BitmapButton(self, -1, wx.Bitmap("images/forward.png", wx.BITMAP_TYPE_ANY))
        
        #self.label_1 = wx.StaticText(self, -1, _("IP Address:"))        
        #self.text_ctrl_ip = wx.TextCtrl(self, -1, "")
       
        
        self.__set_properties()
        self.__do_layout()
        self.__set_events()
        panel = wx.Panel(self, -1)
        panel.Bind(wx.EVT_KEY_DOWN, self.events.OnKeyDown)
        panel.SetFocus()
        
        wx.EVT_MENU(self, ID_ABOUT, self.OnAbout)
        wx.EVT_MENU(self, ID_EXIT, self.TimeToQuit)
        wx.EVT_MENU(self, ID_REBOOT, self.events.reboot_roku)
        wx.EVT_MENU(self, ID_RESET_IP, self.resetRokuIP)
        self.Center()
        # end wxGlade

    def __set_properties(self):
        # begin wxGlade: UIFrame.__set_properties
        self.SetTitle(_("Roku Remote"))
        self.SetSize((280, 400))
        self.SetBackgroundColour(wx.Colour(50, 50, 204))
        self.bitmap_button_back.SetSize(self.bitmap_button_back.GetBestSize())
        self.bitmap_button_home.SetSize(self.bitmap_button_home.GetBestSize())
        self.bitmap_button_up.SetSize(self.bitmap_button_up.GetBestSize())
        self.bitmap_button_left.SetSize(self.bitmap_button_left.GetBestSize())
        self.bitmap_button_ok.SetSize(self.bitmap_button_ok.GetBestSize())
        self.bitmap_button_right.SetSize(self.bitmap_button_right.GetBestSize())
        self.bitmap_button_down.SetSize(self.bitmap_button_down.GetBestSize())
        self.bitmap_button_info.SetSize(self.bitmap_button_info.GetBestSize())
        self.bitmap_button_rewind.SetSize(self.bitmap_button_rewind.GetBestSize())
        self.bitmap_button_play.SetSize(self.bitmap_button_play.GetBestSize())
        self.bitmap_button_forward.SetSize(self.bitmap_button_forward.GetBestSize())
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: UIFrame.__do_layout
        grid_sizer_1 = wx.GridSizer(7, 3, 0, 0)
        grid_sizer_1.Add(self.bitmap_button_back, 0, wx.EXPAND, 0)
        grid_sizer_1.Add((20, 20), 0, 0, 0)
        grid_sizer_1.Add(self.bitmap_button_home, 0, wx.EXPAND, 0)
        grid_sizer_1.Add((20, 20), 0, 0, 0)
        grid_sizer_1.Add(self.bitmap_button_up, 0, wx.EXPAND | wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL, 0)
        grid_sizer_1.Add((20, 20), 0, 0, 0)
        grid_sizer_1.Add(self.bitmap_button_left, 0, wx.EXPAND, 0)
        grid_sizer_1.Add(self.bitmap_button_ok, 0, wx.EXPAND, 0)
        grid_sizer_1.Add(self.bitmap_button_right, 0, wx.EXPAND, 0)
        grid_sizer_1.Add((20, 20), 0, 0, 0)
        grid_sizer_1.Add(self.bitmap_button_down, 0, wx.EXPAND, 0)
        grid_sizer_1.Add((20, 20), 0, 0, 0)
        grid_sizer_1.Add((20, 20), 0, 0, 0)
        grid_sizer_1.Add(self.bitmap_button_info, 0, wx.EXPAND, 0)
        grid_sizer_1.Add((20, 20), 0, 0, 0)
        grid_sizer_1.Add(self.bitmap_button_rewind, 0, wx.EXPAND, 0)
        grid_sizer_1.Add(self.bitmap_button_play, 0, wx.EXPAND, 0)
        grid_sizer_1.Add(self.bitmap_button_forward, 0, wx.EXPAND, 0)
        #grid_sizer_1.Add(self.label_1, 0, wx.EXPAND | wx.ALIGN_RIGHT, 0)
        #grid_sizer_1.Add(self.text_ctrl_ip, 0, wx.EXPAND, 0)
        self.SetSizer(grid_sizer_1)
        self.Layout()
        # end wxGlade
    def __set_events(self):
        self.Bind(wx.EVT_BUTTON, self.events.handle_home, self.bitmap_button_home)
        self.Bind(wx.EVT_BUTTON, self.events.handle_back, self.bitmap_button_back)
        self.Bind(wx.EVT_BUTTON, self.events.handle_up, self.bitmap_button_up)
        self.Bind(wx.EVT_BUTTON, self.events.handle_left, self.bitmap_button_left)
        self.Bind(wx.EVT_BUTTON, self.events.handle_ok, self.bitmap_button_ok)
        self.Bind(wx.EVT_BUTTON, self.events.handle_right, self.bitmap_button_right)
        self.Bind(wx.EVT_BUTTON, self.events.handle_down, self.bitmap_button_down)
        self.Bind(wx.EVT_BUTTON, self.events.handle_info, self.bitmap_button_info)
        self.Bind(wx.EVT_BUTTON, self.events.handle_rewind, self.bitmap_button_rewind)
        self.Bind(wx.EVT_BUTTON, self.events.handle_play, self.bitmap_button_play)
        self.Bind(wx.EVT_BUTTON, self.events.handle_forward, self.bitmap_button_forward)
        
        
    def OnAbout(self, event):
        dlg = wx.MessageDialog(self, "Roku Network Remote\n"
                               "Developed By: Bharat Patel\n"
                               "bharatp@gmail.com",
                               "About Roku Remote", wx.OK | wx.ICON_INFORMATION)
        dlg.ShowModal()
        dlg.Destroy()
    def TimeToQuit(self, event):
         self.Close(True)

    def setIPAddress(self, ip):
        self.host = ip;
        self.statusbar.SetStatusText('Roku IP:%s ' % ip)
        self.events.setIPAddress(ip)
        self.utils.saveRokuAddress(ip)
        
    def resetRokuIP(self, event):
        newIp = self.changeIP()
        self.host = newIp
        #self.setIPAddress(newIp)
        
    def changeIP(self):
        a = wx.GetTextFromUser("Please enter IP Address for your Roku", "IP Address", self.host)
        if len(a) == 0:
            self.Close(True)
            return
        
        if (self.utils.validateIP(a)):
            self.setIPAddress(a)
            return a
        else:
            wx.MessageBox('Invalid IP Address', 'Error', wx.OK | wx.CANCEL | wx.CENTRE|wx.ICON_ERROR)
            self.changeIP()



# end of class UIFrame
